package leetcode_200;

/**
 *@author 周杨
 *BestTimeToBuyAndSellStockII_122 可以多次买入和卖出
 *describe:寻找升序数组 用哨兵标记 AC 82%
 *2018年5月22日 下午6:51:30
 */
public class BestTimeToBuyAndSellStockII_122 {

	public static void main(String[] args) {
		BestTimeToBuyAndSellStockII_122 test=new BestTimeToBuyAndSellStockII_122();
		int []num=new int[] {7,1,5,3,6,4};
		int []num1=new int[] {1,2,3,4,5};
		int []num2=new int[] {7,6,4,3,1};
		System.out.println(test.maxProfit(num2));

	}
	
	public int maxProfit(int[] prices) {
        if(prices.length<=1)
        	return 0;
        int buy=-1,res=0;
        for(int i=0;i<prices.length-1;++i) {
        	if(prices[i]<prices[i+1]){
        		if(buy==-1)
        		buy=prices[i];
        	}
        	else {
        		if(buy!=-1) {
        		res+=prices[i]-buy;
        		buy=-1;
        		}
        	}
        }
        return buy==-1?res:res+prices[prices.length-1]-buy;
    }

}
